//
//  LDMicroCompanyHeadView.swift
//  DingDongZP
//
//  Created by 三秒文化 on 16/7/13.
//  Copyright © 2016年 51ledian.com. All rights reserved.
//

import UIKit

class LDMicroCompanyHeadView: UIView {

    var cycleView:CyclePictureView?
    
    var cellBgView:UIView?

    
    var flagLab = UILabel.setDetailsLab("", fontSize: 14, titleColor: LDFontColor.White, Alignment: .Right)
    
    var companyLab:UILabel?
    var companyWebsite:UILabel?
    
    var companyIndustryImage:UIImageView?
    var companyIndustry:UILabel?
    
    var companySizeImage:UIImageView?
    var companySize:UILabel?
    
    var companyAddressImage:UIImageView?
    var companyAddress:UILabel?
    
    
    var cycleImgH:CGFloat = 220
    var cellH:CGFloat = 115

    
    var model:DDCompanyInfo? {
        
        didSet{
            
            updateFrame()
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupDefaultContent()
        setDefaultLayout()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    

    
    func updateFrame() {
        
        let url = model?.companyshow.map({ (item) -> String in
            
            let urlModel = item //as? DDCompanyshow ?? DDCompanyshow()
            let logoUrl = LDCommentAPI.getPicApi(urlModel.show_logo ?? "")
            
            return logoUrl
            
        })
        
        if url?.count > 0 {
            
            cycleView?.imageURLArray = url
            cycleView?.timeInterval = 3;
            cycleView?.showPageControl = true
        } else {
            
            cycleView?.localImageArray = ["company_show_default"]
        }
        
        
        
        cycleView?.currentBlock = { [weak self] (index) in
            
            if self?.model?.companyshow.count < 1 {
                
                self?.flagLab.text = ""
            } else {
                
                if index == 0 || index > self?.model?.companyshow.count {
                    return
                }
                self?.flagLab.text = "\(index)/\(self?.model?.companyshow.count ?? 0)"
            }
        }
        
        cycleView?.placeholderImage = UIImage(named: "company_show_default")
        
        companyLab?.text = model?.company_fullname ?? "公司名称"
        companyWebsite?.text = model?.company_website ?? "公司网址"
        
        companyIndustry?.text = model?.company_industry ?? "行业"
        companySize?.text = model?.company_size ?? "人数"
        
        companyAddress?.text = model?.company_address ?? "公司地址"
        
        self.layoutIfNeeded()
        
    }
    
    func setDefaultLayout() {
        
        backgroundColor = LDGolbalColor.BgGoundGray
        cycleView?.backgroundColor = UIColor.lightGrayColor()
        
        flagLab.snp_makeConstraints { (make) in
            
            make.right.equalTo(cycleView!).offset(-15)
            make.bottom.equalTo(cycleView!).offset(-LDMargin_V*1.5)
            make.width.equalTo(120)
            make.height.equalTo(15)
        }
        
        //公司名称
        companyLab?.snp_makeConstraints(closure: { (make) in
            
            make.top.equalTo(20)
            make.left.equalTo(15)
        })
        //公司网站
        companyWebsite?.snp_makeConstraints(closure: { (make) in
            
            make.left.equalTo(companyLab!)
            make.top.equalTo(companyLab!.snp_bottom).offset(LDMargin_H)
            
        })
        // 公司类型
        companyIndustryImage?.snp_makeConstraints(closure: { (make) in
            
            make.top.equalTo((companyWebsite?.snp_bottom)!).offset(LDMargin_H*2)
            make.left.equalTo(companyLab!)
            make.size.equalTo((CGSizeMake(10, 10)))
            
        })
        companyIndustry?.snp_makeConstraints(closure: { (make) in
            
            make.top.equalTo(companyIndustryImage!).offset(-2)
            make.left.equalTo(companyIndustryImage!.snp_right).offset(5)
            make.height.equalTo(15)

        })
        // 公司规模
        companySizeImage?.snp_makeConstraints(closure: { (make) in
            
            make.top.height.equalTo(companyIndustryImage!)
            make.left.equalTo(companyIndustry!.snp_right).offset(LDMargin_H*2)
            make.size.equalTo((CGSizeMake(10, 10)))
        })
        companySize?.snp_makeConstraints(closure: { (make) in
          
            make.top.equalTo(companySizeImage!).offset(-2)
            make.left.equalTo(companySizeImage!.snp_right).offset(5)
            make.height.equalTo(15)

        })
        //  公司地址
        companyAddressImage?.snp_makeConstraints(closure: { (make) in
            
            make.top.equalTo(companyIndustryImage!.snp_bottom).offset(LDMargin_H*2)
            make.left.equalTo(companyIndustryImage!)
            make.size.equalTo((CGSizeMake(10, 10)))
        })
        companyAddress?.snp_makeConstraints(closure: { (make) in
            
            make.top.equalTo(companyAddressImage!).offset(-2)
            make.left.equalTo(companyAddressImage!.snp_right).offset(5)
            make.height.equalTo(15)
        })
        
    }
    
    
    func setupDefaultContent() {
        
        cycleView = CyclePictureView.init(frame: CGRectMake(0, 0, LDFrame.ScreenWidth, cycleImgH), imageURLArray: nil)
        cycleView?.autoScroll = true
        cycleView?.showPageControl = false
        
        
        cellBgView = UIView.init(frame: CGRectMake(0, cycleImgH, LDFrame.ScreenWidth, cellH))
        cellBgView?.backgroundColor = LDGolbalColor.White
        
        companyLab = UILabel.setDetailsLab("公司全称", fontSize: 13, titleColor: LDFontColor.Green, Alignment: .Left)
        companyWebsite = UILabel.setDetailsLab("公司网址", fontSize: 12, titleColor: LDFontColor.DeepGray, Alignment: .Left)
        
        
        
        companyIndustryImage = UIImageView.init(image: UIImage.init(named: "companyIndustry"))
        companyIndustry = UILabel.setDetailsLab("公司行业", fontSize: 12, titleColor: LDFontColor.DeepGray, Alignment: .Left)
        
        companySizeImage = UIImageView.init(image: UIImage.init(named: "companySize"))
        companySize = UILabel.setDetailsLab("公司规模", fontSize: 12, titleColor: LDFontColor.DeepGray, Alignment: .Left)
        
        companyAddressImage = UIImageView.init(image: UIImage.init(named: "地址adress"))
        companyAddress = UILabel.setDetailsLab("公司规模", fontSize: 12, titleColor: LDFontColor.DeepGray, Alignment: .Left)
        
        
        addSubview(cycleView!)
        addSubview(flagLab)
        addSubview(cellBgView!)
        cellBgView!.addSubview(companyLab!)
        cellBgView!.addSubview(companyWebsite!)
        cellBgView!.addSubview(companyIndustryImage!)
        cellBgView!.addSubview(companyIndustry!)
        cellBgView!.addSubview(companySizeImage!)
        cellBgView!.addSubview(companySize!)
        cellBgView!.addSubview(companyAddressImage!)
        cellBgView!.addSubview(companyAddress!)
    }
}
